<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/1/30
 * Time: 下午3:12
 */

load()->func('tpl');

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
    $list = pdo_fetchall("SELECT article.*,category.title as category_title FROM "
        . tablename('bontrip_article') . " as article LEFT JOIN " . tablename('bontrip_category')
        . " as category ON article.category_id = category.id WHERE article.uniacid = '{$uniacid}' and article.deleted_at is null"
        . " ORDER BY id DESC");

} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    if (checksubmit('submit')) {
        $data = array(
            'uniacid' => $uniacid,
            'category_id' => intval($_GPC['category_id']),
            'sort' => intval($_GPC['sort']),
            'title' => $_GPC['title'],
            'description' => $_GPC['description'],
            'thumb' => $_GPC['thumb'],
            'link' => $_GPC['link'],
            'is_show' => intval($_GPC['is_show']),
            'updated_at' => TIMESTAMP
        );
        if (!empty($id)) {
            pdo_update('bontrip_article', $data, array('id' => $id));
        } else {
            $data['created_at'] = $data['updated_at'];
            pdo_insert('bontrip_article', $data);
            $id = pdo_insertid();
        }
        message('保存文章成功！', $this->createWebUrl('article', array('op' => 'display')), 'success');
    }
    $article = pdo_fetch("select * from " . tablename('bontrip_article') . " where id={$id} and uniacid={$uniacid}");
    $categorys = pdo_fetchall("SELECT * FROM " . tablename('bontrip_category')
        . " WHERE uniacid = '{$uniacid}' and deleted_at is null ORDER BY id DESC");

} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $article = pdo_fetch("SELECT id  FROM " . tablename('bontrip_article')
        . " WHERE id={$id} AND uniacid={$uniacid} AND deleted_at is null");
    if (empty($article)) {
        message('抱歉，文章不存在或是已经被删除！', $this->createWebUrl('article', array('op' => 'display')), 'error');
    }
    pdo_update('bontrip_article', array('deleted_at' => time()), array('id' => $id));
    message('文章删除成功！', $this->createWebUrl('article', array('op' => 'display')), 'success');

} else {
    message('请求方式不存在');

}
include $this->template('article', TEMPLATE_INCLUDEPATH, true);